const Grade = require("../models/Grade");
const Student = require("../models/student")
// Student.belongsTo(Grade)
Grade.hasMany(Student)
exports.addGrade = async function(gradeObj){
    const ins = await Grade.create(gradeObj);
    return ins.toJSON(); 
}

exports.delGrade = async function(id){
    const ins = await Grade.destroy({
        where:{
            id
        }
    });
    return ins
}

exports.updateGrade = async function(id,updateObj){
    const ins = await Grade.update(updateObj,{
        where:{
            id
        }
    })
    return ins;
}

// 得到所有班级
exports.getGrades = async function(page=1,size=5){
    const { count, rows} = await Grade.findAndCountAll({
        offset:(page-1)*size,
        limit:size
    })
    return {
        count,
        datas:JSON.parse(JSON.stringify(rows))
    }
}

// 根据id查询班级 并且包含学生信息
exports.getGradeInfo = async function(id){
    const res = await Grade.findOne({
        where:{id},
        include:[Student]
    })

    return JSON.parse(JSON.stringify(res))

}